﻿using System;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Share;

namespace ASPNETVN.PORTAL.Modules.DOWNLOAD.Controls
{
    public partial class SimpleAddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += SimpleAddEditItem_Update;
            this.Delete += SimpleAddEditItem_Delete;
            base.OnInit(e);
        }

        void SimpleAddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Components.Item.DeleteItem(this.ItemID);
                this.ReturnToReferrerPage();
            }
        }

        void SimpleAddEditItem_Update(object sender, EventArgs e)
        {
            int categoryID = cbCategory.SelectedValue == string.Empty ? 0 : Convert.ToInt32(cbCategory.SelectedValue);
            Components.Item item = new Components.Item();
            item.CategoryID = categoryID;
            item.Title = txtTitle.Text;
            item.SubTitle = txtSubTitle.Text;
            item.Description = Server.HtmlEncode(txtDescription.Value);
            item.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;

            if (this.ItemID == 0)
            {
                item.CreatedDate = DateTime.Now;
                this.ItemID = item.Insert();
            }
            else
            {
                item.CreatedDate = DateTime.Parse(lblCreationTime.Text);
                item.ID = this.ItemID;
                item.Update();
            }
            string url = HttpUrlBuilder.BuildShareUrl(this.ModuleID, this.ItemID, "UploadFile", "");
            Response.Redirect(url);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Category.BuildDropDownListControl(ref cbCategory, this.ModuleID);

                lblCreationTime.Text = DateTime.Today.ToShortDateString();
                lblCreatedBy.Text = PortalSecurity.CurrentUser.Identity.Email;

                if (this.ItemID > 0)
                {
                    Components.Item item = Components.Item.Load(this.ItemID);
                    if (item != null)
                    {
                        cbCategory.SelectedValue = item.CategoryID.ToString();
                        txtTitle.Text = item.Title;
                        txtSubTitle.Text = item.SubTitle;
                        txtDescription.Value = Server.HtmlDecode(item.Description);
                        lblCreatedBy.Text = item.CreatedBy;
                        lblCreationTime.Text = item.CreatedDate.ToString();
                    }
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }
    }
}